// pages/address-detail/address-detail.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { eidtMemberAddr, addMemberAddr, getAddrDetail } from '../../static/api/api.js'
//获取应用实例
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShowAddress: false,
        address: '',
        addressArr: [],
        addressName: '',
        phone: '',
        curLocation:'',
        addressDetail: '',
        is_default: 0,
        pageStatus: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.address_id) {
            this.setData({
                pageStatus: 'edit',
                address_id: options.address_id
            })
            this.getAddressDetail()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // let locationData = wx.getStorageSync('locationData')
        // if (locationData) {
        //     this.setData({
        //         'curLocation': locationData,
        //         'address': locationData.address
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    switch1Change: function(e) {
        this.setData({
            is_default: e.detail.value ? 1 : 0
        })
    },
    closeAddrPop(data) {
        let result = data.detail;
        this.setData({
            isShowAddress: false
        })
        if (result.type == 2) {
            let arrStr = [];
            result.array.forEach((el, index) => {
                arrStr.push(el.n)
            })
            this.setData({
                addressArr: result.array,
                address: arrStr.join('/')
            })
        }
    },
    //出现选择地址弹窗
    showAddressPop() {
        this.setData({
            isShowAddress: true
        })
    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    //输入框变化
    inputValueChange(e) {
        let dataSet = e.target.dataset;
        let key = dataSet.key
        this.setData({
            [key]: e.detail.value
        })
    },
    //新增地址
    submitAddress() {
        let that = this
        if (this.data.addressName == '') {
            wx.showToast({
                icon: 'none',
                title: '请填写联系人',
            })
            return;
        }
        if (this.data.phone == '') {
            wx.showToast({
                icon: 'none',
                title: '请填写联系电话',
            })
            return;
        }

        if (!utils.isPhone(this.data.phone)) {
            wx.showToast({
                icon: 'none',
                title: '请填写正确的手机号码',
            })
            return;
        }
        if (this.data.address == '') {
            wx.showToast({
                icon: 'none',
                title: '请选择区域',
            })
            return;
        }
        if (this.data.addressDetail == '') {
            wx.showToast({
                icon: 'none',
                title: '请填写详细地址',
            })
            return;
        }
        let url = '';
        
        let params = {
            address: this.data.addressDetail,
            addressee: this.data.addressName,
            contact_phone: this.data.phone,
            region_name: this.data.address,
            region_name_arr: JSON.stringify(this.data.curLocation),
            is_default: this.data.is_default,
            id: this.data.address_id || ''
        };
        let params1 = {
            address: this.data.addressDetail,
            addressee: this.data.addressName,
            contact_phone: this.data.phone,
            region_name: this.data.address,
            region_name_arr: JSON.stringify(this.data.curLocation),
            is_default: this.data.is_default
        };
        wx.showLoading({
            title: '加载中...',
        })
        if (this.data.pageStatus == 'edit') {
            eidtMemberAddr(params).then(response => {
                console.log(response)
                wx.hideLoading();
                let text = '';
                text = "地址修改成功"
                wx.showToast({
                    icon: 'none',
                    title: text,
                })
                setTimeout(function () {
                    wx.navigateBack()
                }, 500)
            }).catch(response => {
                wx.hideLoading();
            })
        } else {
            addMemberAddr(params1).then(response => {
                console.log(response)
                wx.hideLoading();
                let text = '';
                text = '新增地址成功...'
                wx.showToast({
                    icon: 'none',
                    title: text,
                })
                setTimeout(function () {
                    wx.navigateBack()
                }, 500)
            }).catch(response => {
                wx.hideLoading();
            })
        }
        
        
    },
    formatAddr(arr){
        let str = '';
        arr.forEach((el,index)=>{
            str+=el.d+':'+el.n;
            if(index<arr.length-1){
                str += ';'
            }
            
        })
        return str;
    },
    //获取地址详情
    getAddressDetail() {
        let that = this
        wx.showLoading({
            title: '数据加载中...',
        })
        getAddrDetail({ id: this.data.address_id}).then(response => {
            console.log(response)
            wx.hideLoading();
            let data = response.data
            that.setData({
                addressDetail: data.address,
                addressName: data.addressee,
                phone: data.contact_phone,
                address: data.region_name,
                addressArr: data.region_name_arr,
                is_default: data.is_default

            })
        }).catch(response => {
            wx.hideLoading();
        })
       
    }
})